<?php
/**
 * Contact Model
 *
 * @property Contact
 */
class Contact extends AppModel {
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'fname';

    /**
     * associations
     *
     * @var array
    **/


    var $hasMany = array(
        'SmsOutbox' => array(
            'className' => 'SmsOutbox',
            'foreignKey' => 'contact_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    var $belongsTo = array(
        'ContactGroup' => array(
            'className' => 'ContactGroup',
            'foreignKey' => 'contact_group_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        ),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        )
    );
    
    
    function getContactById($id = null){
        $conditions = array(
           'Contact.id' => $id,
           'Contact.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return  $this->find('first', array('conditions' => $conditions, 'recursive' => -1));
    }

    function deleteContacts($ids = null, $user_id){
        $result = $this->updateAll(
            array('Contact.deleted' => "'y'"),
            array('Contact.id' => $ids,'Contact.user_id' => $user_id)
        );
        return $result;
    }


    function getContactForSMS($user_id = null){
        $data = $this->find('all', array(
            'conditions' => array('Contact.user_id' => $user_id,'Contact.deleted' => 'n'),
            'fields' => array('Contact.id', 'Contact.fname', 'Contact.lname', 'Contact.phone_number', 'Contact.country_code','Contact.contact_group_id'),
            'recursive' => -1
        ));

        return $data;
    }

    function getContactGroupForSMS($user_id = null,$contact_group_id){
        $data = $this->find('all', array(
            'conditions' => array('Contact.user_id' => $user_id,'Contact.contact_group_id' => $contact_group_id,'Contact.deleted' => 'n'),
            'fields' => array('Contact.id','Contact.phone_number', 'Contact.country_code'),
            'recursive' => -1
        ));

        return $data;
    }


    function assignGroup($contacts_ids,$group_id,$user_id){
        $result = $this->updateAll(
            array('Contact.contact_group_id' => $group_id),
            array('Contact.id' => $contacts_ids,'Contact.user_id' => $user_id)
        );
        return $result;
    }


    function getTotal($user_id = null){
        $data = $this->find('count', array(
            'conditions' => array('Contact.user_id' => $user_id,'Contact.deleted' => 'n'),
            'recursive' => -1
        ));
        return $data;
    }



}
